<template>
  <div class="set">
    <Row type="flex"
         align="middle"
         justify="center"
         class="set_ct">
      <Col>
      <Form ref="formDynamic"
            :model="formDynamic"
            :rules="ruleValidate"
            :label-width="160">
        <FormItem label="会员副卡押金(币)设值："
                  prop='notMember'>
          <!-- <InputNumber :max="99" :min="0" v-model="formDynamic.notMember" style="width:280px"></InputNumber> -->
          <!-- <InputNumber style="width:280px" placeholder='输入范围为0到99之间'
              :max="99"
              :min="0"
              v-model="formDynamic.notMember"
              :formatter="value => {let val = Math.floor(value); return val}"
              ></InputNumber> -->
          <Input placeholder='输入范围为0到99之间'
                 style="width:280px"
                 :maxlength="2"
                 v-model="formDynamic.notMember"
                 @on-blur="changeblur"
                 @on-keyup='KeyUp'></Input>
          <span style="margin-left:10px">枚</span>
        </FormItem>
        <FormItem>
          <Button type="primary"
                  @click="handleSubmit('formDynamic')"
                  class="button">确定</Button>
          <Button @click="cancel"
                  class="button btn1">取消</Button>
        </FormItem>
      </Form>
      </Col>
    </Row>
  </div>
</template>
<script>
import './index.less'
import { routeEqual } from '@/libs/util'
import { getDicQuery, setDicSave } from '@/api/setting'
export default {
  data () {
    // const validatePass = (rule, value, callback) => {
    //   if (value === null) {
    //     callback(new Error('押金值不能为空'))
    //   } else if (!/^[1-9][0-9]$/.test(value)) {
    //     callback(new Error('输入范围为1到99之间'))
    //   } else {
    //     callback()
    //   }
    // }
    return {
      formDynamic: {
        notMember: '3'
      },
      ruleValidate: {
        notMember: [
          { required: true, message: '押金值不能为空', trigger: 'blur' },
          { required: true, pattern: /^[0-9]|[1-9]{0,2}$/, trigger: 'blur', message: '输入范围为0到99之间' }
        ]
      },
      paramter: {
        'merchantId': this.$store.state.user.merchantId,
        'msType': '0'
      }
    }
  },
  methods: {
    KeyUp (e) {
      if (this.formDynamic.notMember.length === 1) {
        this.formDynamic.notMember = this.formDynamic.notMember.replace(/[^0-9]/g, '')
      } else {
        this.formDynamic.notMember = this.formDynamic.notMember.replace(/^(0+)|[^\d]+/g, '')
      }
    },
    changeblur () {
      if (this.formDynamic.notMember.length === 1) {
        this.formDynamic.notMember = this.formDynamic.notMember.replace(/[^0-9]/g, '')
      } else {
        this.formDynamic.notMember = this.formDynamic.notMember.replace(/^(0+)|[^\d]+/g, '')
      }
    },
    handleSubmit (name) {
      this.$refs[name].validate((valid) => {
        if (valid) {
          let data = Object.assign({}, this.paramter, { 'memberEwalletCardDeposit': String(this.formDynamic.notMember) })
          setDicSave(data).then((res) => {
            if (res.data.status === 200) {
              this.$Message.success('设置成功!')
              this.closeModal()
            } else {
              this.$Message.error(`${res.data.error}`)
            }
          })
        }
      })
    },
    // 关闭弹窗
    closeModal () {
      this.$store.commit('closeTag', this.$route)
      this.$store.commit('setTagNavList', this.$store.state.app.tagNavList.filter(item => !routeEqual(this.$route, item)))
      this.$nextTick(() => {
        this.$router.push({
          name: 'global_setting'
        })
      })
    },
    cancel () {
      // this.$refs.formDynamic.resetFields()
      // this.formDynamic.notMember = null
      this.closeModal()
    }
  },
  mounted () {
    let obj = Object.assign({}, this.paramter, { 'dictKeys': 'memberEwalletCardDeposit' })
    getDicQuery(obj).then((res) => {
      if (res.data.status === 200) {
        this.formDynamic.notMember = String(res.data.data[0].memberEwalletCardDeposit)
      } else {
        this.$Message.error(`${res.data.error}`)
      }
    })
    if (!this.hasPermission('moms:globalsetting:secondarycarddeposit')) {
      this.$router.replace({ name: 'error_401' })
    }
  }
}
</script>
